'''
json.loads():解析一个有效的JSON字符串并将其转换为Python对象(Python字典)
json.load():从一个文件读取JSON类型的数据,然后转转换成Python对象(Python字典)
json.dump:主要用来将python对象(字典)写入json文件
json.dumps:将python对象格式(字典)化成json字符
'''

import ujson
import modem
from modem import net
import app_adc
import app_server_aliyun

cfgpath= "/haas/cfg.json"
def read_cfg():
    result={}
    with open(cfgpath,'r') as f:
        json_data=ujson.load(f)


    if json_data["workMode"]==0:
        # 透传模式
        result["name"]=json_data["name"]
        result["id"]=json_data["id"]
        result["uploadInterval"]=json_data["uploadInterval"]
        result["adcType"]=json_data["adcType"]
        result["aiMode1"]=json_data["aiMode1"]
        result["diMode1"]=json_data["diMode1"]
        result["aiMode2"]=json_data["aiMode2"]
        result["diMode2"]=json_data["diMode2"]
        result["subcontractInterval"]=json_data["subcontractInterval"]

        result["heartBeat"]=json_data["heartBeat"]
        result["deviceName"]=json_data["workMode1"]["aliyun_config"]["deviceName"]
        result["productKey"]=json_data["workMode1"]["aliyun_config"]["productKey"]
        result["productSecret"]=json_data["workMode1"]["aliyun_config"]["productSecret"]
        result["region"]=json_data["workMode1"]["aliyun_config"]["region"]

        
        result["rs232"]={}    
        result["rs232"]["enable"]=json_data["en_rs232"]
        result["rs232"]["baudRate"]=json_data["workMode1"]["rs232_config"]["baudRate"]
        result["rs232"]["dataWidth"]=json_data["workMode1"]["rs232_config"]["dataWidth"]
        result["rs232"]["stopBits"]=json_data["workMode1"]["rs232_config"]["stopBits"]
        result["rs232"]["parity"]=json_data["workMode1"]["rs232_config"]["parity"]
        result["rs485"]={}
        result["rs485"]["enable"]=json_data["en_rs485"]
        result["rs485"]["baudRate"]=json_data["workMode1"]["rs485_config"]["baudRate"]
        result["rs485"]["dataWidth"]=json_data["workMode1"]["rs485_config"]["dataWidth"]
        result["rs485"]["stopBits"]=json_data["workMode1"]["rs485_config"]["stopBits"]
        result["rs485"]["parity"]=json_data["workMode1"]["rs485_config"]["parity"]

        return result,0
    elif  json_data["workMode"]==1:
        # 自定义模式
        result["name"]=json_data["name"]
        result["id"]=json_data["id"]
        result["uploadInterval"]=json_data["uploadInterval"]
        result["heartBeat"]=json_data["heartBeat"]
        if json_data["workMode2"]["enableOption"]==1:
            result["deviceName"]=json_data["option1"]["aliyun_config"]["deviceName"]
            result["productKey"]=json_data["option1"]["aliyun_config"]["productKey"]
            result["productSecret"] =json_data["option1"]["aliyun_config"]["productSecret"]
            result["region"]=json_data["option1"]["aliyun_config"]["region"]
            return  result,1

        elif json_data["workMode2"]["enableOption"]==2:
            result["ip"]=json_data["option2"]["tcp_config"]["ip"]
            result["port"]=json_data["option2"]["tcp_config"]["port"]
            return result,2
 
        elif  json_data["workMode2"]["enableOption"]==3:
            result["clientId"]=json_data["option3"]["mqtt_config"]["clientId"]
            result["userName"]=json_data["option3"]["mqtt_config"]["userName"]
            result["password"] =json_data["option3"]["mqtt_config"]["password"]
            result["mqttAddr"]=json_data["option3"]["mqtt_config"]["mqttAddr"]
            result["port"] =json_data["option3"]["mqtt_config"]["port"]
            result["get_topic"]=json_data["option3"]["mqtt_config"]["get_topic"] 
            result["post_topic"]=json_data["option3"]["mqtt_config"]["post_topic"]   
            result["qos"]=json_data["option3"]["mqtt_config"]["qos"]                                                  
            result["region"]=json_data["option3"]["mqtt_config"]["region"]
            return  result,3            


def write_read_cfg(data):
    with open(cfgpath,'r') as fr:
        json_data = ujson.load(fr)
    # print (json_data)    
    
    # 本地读取配置
    result={}
    if "ask" in data:
        # 读取参数
        # adc类型，上报间隔，设备名称，设备id，AI上报模式，DI上报模式
        if "basecfg" in data["ask"] and data["ask"]["basecfg"]=="1":
            result["adcType"]=json_data["adcType"]
            result["name"]=json_data["name"]
            result["id"]=json_data["id"]
            result["aiMode1"]=json_data["aiMode1"]
            result["aiMode2"]=json_data["aiMode2"]
            result["diMode1"]=json_data["diMode1"]
            result["diMode2"]=json_data["diMode2"]
            result["uploadInterval"]=json_data["uploadInterval"]

            return result

        # 引擎软件版本，应用软件版本，IMEI、ICCID、供电电压、信号值、AI数据、DI数据
        if "basedata" in data["ask"] and data["ask"]["basedata"]=="1":
            FwVersion=modem.info.getDevFwVersion()
            result["fwVersion"]=FwVersion[:FwVersion.find(",")]
            # ----------------------------需要修改
            result['appVersion']=app_server_aliyun.get_app_version()
            result["imei"]=modem.info.getDevImei()
            result["iccid"]=modem.sim.getIccid()
            result["csq"]=net.getCsq()
            result["aiData1"]=app_adc.get_ai_current_value()[0]
            result["aiData2"]=app_adc.get_ai_current_value()[1]
            result["diData1"]=app_adc.get_di_value()[0]
            result["diData2"]=app_adc.get_di_value()[1]
            return result

        # 232(使能、波特率、数据位、停止位、校验位),485(使能、波特率、数据位、停止位、校验位)
        if "comcfg" in data["ask"] and data["ask"]["comcfg"]=="1":
            result["rs232"]={}
            result["rs232"]["enable"]=json_data["en_rs232"]
            result["rs232"]["baudRate"]=json_data["workMode1"]["rs232_config"]["baudRate"]
            result["rs232"]["dataWidth"]=json_data["workMode1"]["rs232_config"]["dataWidth"]
            result["rs232"]["stopBits"]=json_data["workMode1"]["rs232_config"]["stopBits"]
            result["rs232"]["parity"]=json_data["workMode1"]["rs232_config"]["parity"]
            result["rs485"]={}
            result["rs485"]["enable"]=json_data["en_rs485"]
            result["rs485"]["baudRate"]=json_data["workMode1"]["rs485_config"]["baudRate"]
            result["rs485"]["dataWidth"]=json_data["workMode1"]["rs485_config"]["dataWidth"]
            result["rs485"]["stopBits"]=json_data["workMode1"]["rs485_config"]["stopBits"]
            result["rs485"]["parity"]=json_data["workMode1"]["rs485_config"]["parity"]
            return result
        # deviceName、productKey、productSecret、heartBeat、region
 
        if "aliyuncfg" in data["ask"] and data["ask"]["aliyuncfg"]=="1":
            result["deviceName"]=json_data["workMode1"]["aliyun_config"]["deviceName"]
            result["productKey"]=json_data["workMode1"]["aliyun_config"]["productKey"]
            result["productSecret"]=json_data["workMode1"]["aliyun_config"]["productSecret"]
            result["heartBeat"]=json_data["heartBeat"]
            result["region"]=json_data["workMode1"]["aliyun_config"]["region"]
            return result
    else:
        #本地写参数
    #{"basecfg":{"adcType":0,uploadInterval":8,"name":"HD1",
    # "id":164544534,"aimode1":0,"dimode1":0,"aimode1":0,"dimode1":0}}
        if "basecfg" in data :
            json_data["id"]=data["basecfg"]["id"]
            json_data["name"]=data["basecfg"]["name"]
            json_data["adcType"]=data["basecfg"]["adcType"]
            json_data["dimode1"]=data["basecfg"]["dimode1"]
            json_data["aimode1"]=data["basecfg"]["aimode1"]
            json_data["dimode2"]=data["basecfg"]["dimode2"]
            json_data["aimode2"]=data["basecfg"]["aimode2"]        
            json_data["uploadInterval"]=data["basecfg"]["uploadInterval"]
            # json_data["subcontractInterval"]=data["basecfg"]["subcontractInterval"]


        # {"aliyun_config":{"deviceName":"867082058706912","productKey":"a1xfKxvoUo",
        # "productSecret":"KqYUeD4BxGSM9eQ","heartBeat":60,"region":"cn-shanghai"}}  
        if "aliyun_config" in data:
            json_data["workMode1"]["aliyun_config"]["deviceName"]=data["aliyun_config"]["deviceName"]
            json_data["workMode1"]["aliyun_config"]["productKey"]=data["aliyun_config"]["productKey"]
            json_data["workMode1"]["aliyun_config"]["productSecret"]=data["aliyun_config"]["productSecret"]
            json_data["workMode1"]["aliyun_config"]["region"]=data["aliyun_config"]["region"]
            json_data["heartBeat"]=data["aliyun_config"]["heartBeat"]
        
        # 备份rs232配置 到cfg.json中 
        # {"rs232":{"enable":1,dataWidth":8,
        #    "baudRate":9600,"stopBits":1,"parity":"none"}}
        if "rs232" in data :
            json_data["en_rs232"]=data["rs232"]["enable"]
            json_data["workMode1"]["rs232_config"]["dataWidth"]=data["rs232"]["dataWidth"]
            json_data["workMode1"]["rs232_config"]["baudRate"]=data["rs232"]["baudRate"]
            json_data["workMode1"]["rs232_config"]["stopBits"]=data["rs232"]["stopBits"]
            json_data["workMode1"]["rs232_config"]["parity"]=data["rs232"]["parity"]
        # 备份rs485配置 到cfg.json中 
        # {"rs485":{"enable":1,dataWidth":8,"baudRate":9600,"stopBits":1,"parity":"none"}}
        if "rs485" in data :
            json_data["en_rs485"]=data["rs485"]["enable"]
            json_data["workMode1"]["rs485_config"]["dataWidth"]=data["rs485"]["dataWidth"]
            json_data["workMode1"]["rs485_config"]["baudRate"]=data["rs485"]["baudRate"]
            json_data["workMode1"]["rs485_config"]["stopBits"]=data["rs485"]["stopBits"]
            json_data["workMode1"]["rs485_config"]["parity"]=data["rs485"]["parity"]  
        


    # 云端写入配置参数
        if "name" in data :
            json_data["name"]=data["name"]
        if "id" in data:
            json_data["id"]=data["id"]    
        if "adcType" in data:
            json_data["adcType"]=data["adcType"]
        if "diMode" in data:
            json_data["diMode1"]=data["diMode"]["diMode1"]   
            json_data["diMode2"]=data["diMode"]["diMode2"]         
        if "aiMode" in data:
            json_data["aiMode1"]=data["aiMode"]["aiMode1"]
            json_data["aiMode2"]=data["aiMode"]["aiMode2"]
        if "subcontractInterval" in data:
            json_data["subcontractInterval"]=data["subcontractInterval"]            
        if "uploadInterval" in data:
            json_data["uploadInterval"]=data["uploadInterval"]
        if "heartBeat" in data:
            json_data["heartBeat"]=data["heartBeat"]
        if "rs232Config" in data:
            json_data["en_rs232"]=data["rs232Config"]["enable"]
            json_data["workMode1"]["rs232_config"]["dataWidth"]=data["rs232Config"]["dataWidth"]
            json_data["workMode1"]["rs232_config"]["baudRate"]=data["rs232Config"]["baudRate"]
            json_data["workMode1"]["rs232_config"]["stopBits"]=data["rs232Config"]["stopBits"]
            json_data["workMode1"]["rs232_config"]["parity"]=data["rs232Config"]["parity"]

        if "rs485Config" in data:
            json_data["en_rs485"]=data["rs485Config"]["enable"]
            json_data["workMode1"]["rs485_config"]["dataWidth"]=data["rs485Config"]["dataWidth"]
            json_data["workMode1"]["rs485_config"]["baudRate"]=data["rs485Config"]["baudRate"]
            json_data["workMode1"]["rs485_config"]["stopBits"]=data["rs485Config"]["stopBits"]
            json_data["workMode1"]["rs485_config"]["parity"]=data["rs485Config"]["parity"]

        # print (json_data)
        with open(cfgpath,'w') as fw:
            fw.write(ujson.dumps(json_data))



            



def write_read_board(data):

    with open("/data/board.json",'r') as fr:
        json_data = ujson.load(fr)
    # print (json_data)

    # 本地写入rs232配置
    # {"rs232":{"enable":1,dataWidth":8,
    #    "baudRate":9600,"stopBits":1,"parity":"none"}}
    if "rs232" in data :
        json_data["io"]["RS232"]["dataWidth"]=data["rs232"]["dataWidth"]
        json_data["io"]["RS232"]["baudRate"]=data["rs232"]["baudRate"]
        json_data["io"]["RS232"]["stopBits"]=data["rs232"]["stopBits"]
        json_data["io"]["RS232"]["parity"]=data["rs232"]["parity"]
   # 写入rs485配置
   # {"rs485":{"enable":1,dataWidth":8,"baudRate":9600,"stopBits":1,"parity":"none"}}
    if "rs485" in data:
        json_data["io"]["RS485"]["dataWidth"]=data["rs485"]["dataWidth"]
        json_data["io"]["RS485"]["baudRate"]=data["rs485"]["baudRate"]
        json_data["io"]["RS485"]["stopBits"]=data["rs485"]["stopBits"]
        json_data["io"]["RS485"]["parity"]=data["rs485"]["parity"]             
    # print (json_data)

    # 云端配置
    if "rs232Config" in data:
        json_data["io"]["RS232"]["dataWidth"]=data["rs232Config"]["dataWidth"]
        json_data["io"]["RS232"]["baudRate"]=data["rs232Config"]["baudRate"]
        json_data["io"]["RS232"]["stopBits"]=data["rs232Config"]["stopBits"]
        json_data["io"]["RS232"]["parity"]=data["rs232Config"]["parity"]
    if "rs485Config" in data:
        json_data["io"]["RS485"]["dataWidth"]=data["rs485Config"]["dataWidth"]
        json_data["io"]["RS485"]["baudRate"]=data["rs485Config"]["baudRate"]
        json_data["io"]["RS485"]["stopBits"]=data["rs485Config"]["stopBits"]
        json_data["io"]["RS485"]["parity"]=data["rs485Config"]["parity"]

    with open("/haas/board.json",'w') as fw:
        fw.write(ujson.dumps(json_data))






